import $H from './request.js'
export default {
	// 更新tabbar角标
	updateCartBadge(count){
		if(count > 0){
			uni.setTabBarBadge({
				index:2,
				text:count.toString()
			})
		}else{
			uni.removeTabBarBadge({
				index:2
			})
		}
		
	},
	// 监听网络
	onNetWork(){
		let func = (res)=>{
			if (res.networkType === 'none') {
				uni.showToast({
					title: '当前处于断网状态,请先连接',
					icon: 'none'
				});
			}
		}
		uni.getNetworkType({
		    success:func
		});
		uni.onNetworkStatusChange(func);
	},
	// 数组置顶
	__toFirst(arr,index){
		if (index != 0) {
			arr.unshift(arr.splice(index,1)[0]);
		}
		return arr;
	},
	// userinfo格式转换
	fixed(x,n){
		let N = Math.pow(10, n);
		return Math.round(x * N) / N;
	},
	// exp: 6 -> 6.00 
	formatyuan(x){
		let N = Math.pow(10, 2)
		let d = Math.round(x * N) / N
		let s = d.toString()
		let rs = s.indexOf('.')
		if(rs<0){
			rs = s.length
			s+='.'
		}
		while(s.length <= rs+2){
			s+='0'
		}
		return s
	},
	generateUUID(){
		return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c)=> {
		        let r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8)
		        return v.toString(16);
		})
	},
	toFixed(num){
		if (isNaN(num)) {
		 return "NaN";
		}
		return num.toFixed(2);
	},
	getFileExtension(filePath){
		const fileExtension = filePath.split('.').pop();
		return fileExtension; // 输出：txt
	}
}